package blackjack;

public class Mazzo {

    private Carta[] mazzo = new Carta[52];
    private int[] carte = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,41,42,43,44,45,46,47,48,49,50,51,52};
    private Carta[] mischia = new Carta[52];

    public Mazzo() {
        for (int i = 0; i < 52; i++) {
            String seme;
            int num;
            if (i < 13) {
                seme = "Picche";
                num = i + 1;
            } else if (i < 26) {
                seme = "Fiori";
                num = i + 1 - 13;
            } else if (i < 39) {
                seme = "Quadri";
                num = i + 1 - 26;
            } else {
                seme = "Cuori";
                num = i + 1 - 39;
            }
            int valore=num;
            if(num==1){
                valore=11;
            }
            if(num==11 || num==12 || num==13){
                valore=10;
            }
            this.mazzo[i] = new Carta();
            this.mischia[i] = new Carta();
            this.mazzo[i].setSeme(seme);
            this.mazzo[i].setValore(valore);
            this.mazzo[i].setNumero(num+"");
        }
    }

    public void shuffle() {
        int[] card;
        for (int i = 0; i < 52; i++) {
            card = new int[52 - i];
            int num=0;
            for (int id = 0; id < this.carte.length; id++) {
                if (this.carte[id] != 0) {
                    card[num] = this.carte[id];
                    num++;
                }
            }
            double rand = Math.random();
            rand = rand * card.length;
            int randint = (int) rand;
            int pos = card[randint];
            pos--;
            this.carte[pos] = 0;
            this.mischia[i].setValore(this.mazzo[pos].getValore());
            this.mischia[i].setSeme(this.mazzo[pos].getSeme());
            this.mischia[i].setNumero(this.mazzo[pos].getNumero());
        }
        for(int i=0; i<52; i++){
            this.mazzo[i]=this.mischia[i];
        }
    }

    public Carta getCarta(int pId){
        if(pId<52){
            return this.mazzo[pId];
        }
        return this.mazzo[0];
    }

}
